// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init()

// 云函数入口函数
exports.main = async (event, context) => {
  const wxContext = cloud.getWXContext()
  try {
    let result = await cloud.openapi.security.imgSecCheck({
      media: {
        contentType: 'image/jpg',
        value: Buffer.from(event) 
      }
    })
    if (result && result.errCode === '0'){
        return { code: 200, msg: 'ok', data: result }
    }
  } catch (err) {
      // 错误处理
    if (err.errCode === '87014') {
        return { code: 500, msg: '图片可能潜在风险', data: err } 
    }
    else if(err.errMsg === 'ok'){
        return { code: 200, msg: '图片正常', data: err }
    }
    else if(err.errMsg === 'risky content'){
        return { code: 500, msg: '内容可能潜在风险', data: err }
    }
    return { code: 502, msg: '调用security接口异常', data: err }
  }
}
